package demo.swing.date;

import java.awt.GridLayout;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;

import demo.swing.FlightReservation;

public class EnterDate extends JFrame {
	private JLabel yearLabel = null;
	private JLabel monathLabel = null;
	private JLabel dayLabel = null;
	private JComboBox<Integer> year = null;
	private JComboBox<Integer> monath = null;
	private JComboBox<Integer> day = null;
	private JButton	okBtn = null;
	public EnterDate(String title, Object p){
		super(title);
//		setClosable(true);
//		setMaximizable(true);
//		setResizable(true);
//		setBounds(150, 150, 250, 250);
		yearLabel = new JLabel("Select year:");
		monathLabel = new JLabel("Select moth:");
		dayLabel = new JLabel("Select day:");
		okBtn = new JButton("Data selected.");
		year = new JComboBox<Integer>();
		monath = new JComboBox<Integer>();
		day = new JComboBox<Integer>();
		getYear().setModel(new IntegerComboBoxModel(new Integer(2012), new Integer (2014)));
		getMonath().setModel(new IntegerComboBoxModel(new Integer(1), new Integer (12)));
		getDay().setModel(new IntegerComboBoxModel(new Integer(1), new Integer (31)));
		if (p != null)
			getOkBtn().addActionListener((ActionListener)p);
		this.setLayout(new GridLayout(4, 2));
		this.add(getYearLabel());
		this.add(getYear());
		this.add(getMonathLabel());
		this.add(getMonath());
		this.add(getDayLabel());
		this.add(getDay());
		this.add(getOkBtn());
		this.pack();
	}
	
	public void setOkBtnActionListener(Object p){
		if (p != null)
			okBtn.addActionListener((ActionListener)p);
		return;
	}
	
	public JButton getOkBtn() {
		return okBtn;
	}
	public JLabel getYearLabel() {
		return yearLabel;
	}
	public JLabel getMonathLabel() {
		return monathLabel;
	}
	public JLabel getDayLabel() {
		return dayLabel;
	}
	public JComboBox<Integer> getYear() {
		return year;
	}
	public JComboBox<Integer> getMonath() {
		return monath;
	}
	public JComboBox<Integer> getDay() {
		return day;
	}
	public String getDate(){
		int year = ((IntegerComboBoxModel)getYear().getModel()).getIntSelectedItem();
		int day = ((IntegerComboBoxModel)getDay().getModel()).getIntSelectedItem();
		int monath = ((IntegerComboBoxModel)getMonath().getModel()).getIntSelectedItem();
		String yearString = null, dayString = null, monathString = null;
		if (day < 10){
			dayString = new String("0" + String.valueOf(day));
		}else{
			dayString = String.valueOf(day);
		}
		if (monath < 10){
			monathString = new String("0" + String.valueOf(monath));
		}else{
			monathString = String.valueOf(monath);
		}
		yearString = String.valueOf(year);
		return new String(yearString + "-" + monathString + "-"  + dayString);
	}
}
